﻿using System;
using System.Windows.Data;

namespace Informant.Clients.UI.Wpf4.Converters
{
	/// <summary>
	/// Use this converter when you want the value of an object to be a percentage of a value of a different object.
	/// The parameter must be a decimal value, for example .01 = 1% and 1 = 100%.
	/// </summary>
	public class PercentageOfValueConverter : IValueConverter
	{
		#region IValueConverter Members

		public Object Convert(Object value, Type targetType, Object parameter, System.Globalization.CultureInfo culture)
		{
			Object result = Binding.DoNothing;
			if (value != null && parameter != null)
			{
				Decimal original;
				Decimal.TryParse(value.ToString(), out original);
				
				Decimal percent;
				Decimal.TryParse(parameter.ToString(), out percent);

				Decimal newValue = original * percent;
				
				result = newValue;
			}
			return result;
		}

		public Object ConvertBack(Object value, Type targetType, Object parameter, System.Globalization.CultureInfo culture)
		{
			throw new NotImplementedException();
		}

		#endregion
	}
}
